layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func','laydate'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate=layui.laydate;


    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        trigger: 'click',
        // max: Feng.currentDate()
    });
    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable"    //表格id
    };
    $("#condition").on("input",function(e){
        //获取input输入的值
        Notice.search();
    });
    form.on('select(dm)', function(data){
        Notice.search();
    });
    /**
     * 选中行效果
     */
    table.on('row(noticeTable)',function(obj){
        $(obj.tr).siblings().css("background-color","");
        obj.tr.css({"background-color":"#dadad9"});
    });
    /**
     * 检索店名
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyXd/getDm");
    var languageResult = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        $("#dm").append("<option value='"+val.dm+"'>"+val.dm+"</option>");
    } );
    form.render('select');
    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        return [[
            {type: 'checkbox',LAY_CHECKED:true,hide:true},
            {field: 'id', align: "center", hide: true, sort: true, title: 'id'},
            // {align: 'center', type: "numbers", title: '序号'},
            {field: 'dm', align: "center", sort: false, title: '店名',width:100,fixed: 'left'},
            {field: 'sum_xd', align: "center", sort: false, title: '得分',width:110},
            {field: 'ctbh', align: "center", sort: false, title: '餐厅编号',width:110},
            {field: 'question', align: "center", sort: false, title: '问题1',width:110},
            {field: 'gy', align: "center", sort: false, title: '根源',width:100},
            {field: 'gjmb', align: "center", sort: false, title: '改进目标',width:110},
            {field: 'xdjh', align: "center", sort: false, title: '行动计划',width:100},
            {field: 'zrr', align: "center", sort: false, title: '责任人',width:80},
            {field: 'dcsj', align: "center", sort: true, title: '达成时间',width:110},
            {field: 'question_one', align: "center", sort: false, title: '问题2',width:110},
            {field: 'gy_one', align: "center", sort: false, title: '根源',width:100},
            {field: 'gjmb_one', align: "center", sort: false, title: '改进目标',width:110},
            {field: 'xdjh_one', align: "center", sort: false, title: '行动计划',width:100},
            {field: 'zrr_one', align: "center", sort: false, title: '责任人',width:80},
            {field: 'dcsj_one', align: "center", sort: true, title: '达成时间',width:110},
            {field: 'question_two', align: "center", sort: false, title: '问题3',width:110},
            {field: 'gy_two', align: "center", sort: false, title: '根源',width:100},
            {field: 'gjmb_two', align: "center", sort: false, title: '改进目标',width:110},
            {field: 'xdjh_two', align: "center", sort: false, title: '行动计划',width:100},
            {field: 'zrr_two', align: "center", sort: false, title: '责任人',width:130},
            {field: 'dcsj_two', align: "center", sort: true, title: '达成时间',width:110},
            {field: 'question_three', align: "center", sort: false, title: '问题4',width:110},
            {field: 'gy_three', align: "center", sort: false, title: '根源',width:100},
            {field: 'gjmb_three', align: "center", sort: false, title: '改进目标',width:110},
            {field: 'xdjh_three', align: "center", sort: false, title: '行动计划',width:100},
            {field: 'zrr_three', align: "center", sort: false, title: '责任人',width:80},
            {field: 'dcsj_three', align: "center", sort: true, title: '达成时间',width:110},
            {field: 'create_time',templet:"<div>{{layui.util.toDateString(d.create_time, 'yyyy-MM-dd')}}</div>",align: "center", sort: true, title: '创建时间',width:110},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 150}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['timeLimit'] = $("#timeLimit").val();
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加通知
     */
    Notice.openAddNotice = function () {
        func.open({
            height: 1000,
            title: '添加巡店沟通',
            content: Feng.ctxPath + '/btyXdgt/btyXdgt_add',
            tableId: Notice.tableId
        });
    };
    /**
     * 点击删除通知
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onDeleteNotice = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyXdgt/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };
    /**
     * 点击修改档案
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onEditNotice = function (data) {
        func.open({
            height: 1000,
            title: '修改巡店',
            content: Feng.ctxPath + "/btyXdgt/btyXdgt_update/" + data.id,
            tableId: Notice.tableId
        });
    };
    /**
     * 导出excel按钮
     */
    Notice.exportExcel = function () {
        //获取导出Excel的数据
        var ajax = new $ax(Feng.ctxPath + "/btyXdgt/listExport");
        var condition = $("#condition").val();
        var timeLimit = $("#timeLimit").val();
        ajax.set("condition",condition);
        ajax.set("timeLimit",timeLimit);
        var result = ajax.start();
        var checkRows = table.checkStatus(Notice.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            var lists=result;
            for (index in lists) {
                var createTime = lists[index].createTime;
                lists[index].createTime = createTime + "\t";

            }
            table.exportFile(tableResult.config.id, lists, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Notice.tableId,
        url: Feng.ctxPath + '/btyXdgt/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: Notice.initColumn(),
        // done:function (res, curr, count) {
        //     //图片预览
        //     layer.photos({
        //         photos: '.show_img',
        //         anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
        //     });
        // }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Notice.openAddNotice();
    });
    // 导出按钮点击事件
    $('#btnExp').click(function () {
        Notice.exportExcel();
    });


    // 工具条点击事件
    table.on('tool(' + Notice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Notice.onEditNotice(data);
        } else if (layEvent === 'delete') {
            Notice.onDeleteNotice(data);
        }
    });
});
